package jp.sourceforge.akjrcp.util;

import java.io.File;

public class FileSizeMonitor extends Thread{

	private long maxSize;
	private int step;
	
	private boolean running;
	private File file;
	
	private int sleepTime=100;
	private FileSizeListener listener;
	public int getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
	}

	public void stopMonitor(){
		running=false;
	}
	
	public void startMonitor(){
		running=true;
		this.start();
	}

	public void run() {
		long persize=maxSize/step;
		long current=0;
		while(running){
			if(file==null){
				break;
			}
			long size=file.length();
			long v=size/persize;
			if(size>=maxSize){
				listener.done((int)(step-current));
				
				break;
			}
			
			if(v>current){
				listener.stepped((int)(v-current));
				current=v;
			}
			
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		running=false;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public long getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(long maxSize) {
		this.maxSize = maxSize;
	}

	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public FileSizeListener getListener() {
		return listener;
	}

	public void setListener(FileSizeListener listener) {
		this.listener = listener;
	}

}
